package com.mvchib.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mvchib.model.HpBbs;
import com.mvchib.service.HpBbsService;

@Controller
@RequestMapping(value = "/bbs")
public class HpBbsController {

    @Autowired
    private HpBbsService bbsService;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addBbsPage() {
        ModelAndView modelAndView = new ModelAndView("add-bbs-form");
        modelAndView.addObject("bbs", new HpBbs());
        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addingBbs(@ModelAttribute HpBbs bbs) {

        ModelAndView modelAndView = new ModelAndView("home");
        bbsService.addBbs(bbs);

        String message = "Bbs was successfully added.";
        modelAndView.addObject("message", message);

        return modelAndView;
    }

    @RequestMapping(value = "/list")
    public ModelAndView listOfBbss() {
        ModelAndView modelAndView = new ModelAndView("list-of-bbss");

        List<HpBbs> bbss = bbsService.getBbss();
        modelAndView.addObject("bbss", bbss);

        return modelAndView;
    }

    @RequestMapping(value = "/edit/{bid}", method = RequestMethod.GET)
    public ModelAndView editTeamPage(@PathVariable Integer bid) {
        ModelAndView modelAndView = new ModelAndView("edit-bbs-form");
        HpBbs bbs = bbsService.getBbs(bid);
        modelAndView.addObject("bbs", bbs);
        return modelAndView;
    }

    @RequestMapping(value = "/edit/{bid}", method = RequestMethod.POST)
    public ModelAndView edditingTeam(@ModelAttribute HpBbs bbs, @PathVariable Integer bid) {

        ModelAndView modelAndView = new ModelAndView("home");

        bbsService.updateBbs(bbs);

        String message = "Bbs was successfully edited.";
        modelAndView.addObject("message", message);

        return modelAndView;
    }

    @RequestMapping(value = "/delete/{bid}", method = RequestMethod.GET)
    public ModelAndView deleteTeam(@PathVariable Integer bid) {
        ModelAndView modelAndView = new ModelAndView("home");
        bbsService.deleteBbs(bid);
        String message = "Bbs was successfully deleted.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

}
